package com.sdk.auth.aop;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.RSA;
import jakarta.annotation.PostConstruct;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

/**
 * @author wst
 * @date 2023/7/17
 * 敏感数据加解密密钥
 */
@Slf4j
@Data
@Configuration
@ConfigurationProperties(prefix = "secret.sensitive")
public class SensitiveKeyProperties {

    private String privateKey;

    private String publicKey;

    private RSA rsa;

    @PostConstruct
    public void init() {
        try {
            if (StrUtil.isNotBlank(privateKey) && StrUtil.isNotBlank(publicKey)) {
                this.rsa = SecureUtil.rsa(privateKey, publicKey);
            }
        } catch (Exception e) {
            this.rsa = null;
            log.error("SensitiveKeyProperties init 异常: {}", e.getMessage(), e);
        }
        log.debug("SensitiveKeyProperties init:{}", this);
    }
}
